package com.itheima.reggie.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersVo;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("order")
@Service
public class OrderController {


    @Autowired
    private OrderService ordersService;


    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders order){
        ordersService.submit(order);
        return R.success("下单成功");
    }



    @GetMapping("page")
    public R<Page> page(int page, int pageSize, Long number,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        Page<Orders> ordersPage = new Page<Orders>(page, pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper();
        lqw.like(number != null, Orders::getNumber, number);
        lqw.ge(beginTime != null, Orders::getOrderTime, beginTime);
        lqw.le(endTime != null, Orders::getOrderTime, endTime);

        ordersService.page(ordersPage, lqw);

        return R.success(ordersPage);
    }


    /**
     * 导出数据到Excel文件
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("exportExcel")
    public R<String> exportExcel(Long number,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper();
        lqw.like(number != null, Orders::getNumber, number);
        lqw.ge(beginTime != null, Orders::getOrderTime, beginTime);
        lqw.le(endTime != null, Orders::getOrderTime, endTime);

        List<Orders> ordersList = ordersService.list(lqw);

        List<OrdersVo> ordersDtos = new ArrayList<>();
        for (Orders orders : ordersList) {
            OrdersVo dto = new OrdersVo();
            BeanUtils.copyProperties(orders, dto, "status", "payMethod");

            // 订单状态
            String status = "";
            Integer s = orders.getStatus();
            switch (s) {
                case 1:
                    status = "待付款";
                    break;
                case 2:
                    status = "待派送";
                    break;
                case 3:
                    status = "已派送";
                    break;
                case 5:
                    status = "已完成";
                    break;
                default:
                    status = "已取消";
            }
            dto.setStatus(status);

            // 支付方式
            String payMethod = orders.getPayMethod() == 1 ? "微信" : "支付宝";
            dto.setPayMethod(payMethod);

            ordersDtos.add(dto);
        }

        String fileName = "D:/" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, OrdersVo.class)
                .registerConverter(new LongStringConverter())
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("订单详情")
                .doWrite(ordersDtos);

        return R.success("导出成功");
    }
}
